angular.module('app').factory('asciiTableService', ['$translate', function($translate) {
    var asciiTableService = {
        drawAsciiTable:function(columns,row_d){
        	var html = [];
        	var theadHtml ="";
        	var tbodyHtml = [];
        	if(columns.length>0){
        		var thHtml = [];
        		$.each(columns,function(index,data){
        			if(typeof data!="string"){
        				data = JSON.stringify(data);
        			}
        			var th = '<th>'+ data +'</th>';
        			thHtml.push(th);
        		});        		
        		theadHtml = '<thead><tr>' + thHtml.join(" ") + '</tr></thead>';
        	}
        	
        	$.each(row_d,function(index,data){
        		var i = data.length;
        		var tdHtml = [];
        		$.each(data.row,function(idx,d){
        			if(typeof d!="string"){
        				d = JSON.stringify(d);
        			}
        			var td = '<td>'+ d +'</td>';
        			tdHtml.push(td);
        		});
        		var tbodyTr = '<tr>'+ tdHtml.join(" ") +'</tr>';
				tbodyHtml.push(tbodyTr);
        	});

        	var tableHtml = '<table class="asciiTable">' + 
        			theadHtml + 
        			'<tbody>'+
        			tbodyHtml.join(" ")+
        			'</tbody>'+
        			'</table>';

    		return tableHtml;
        },
        drawNormalTable:function(columns,row_d){
        	var html = [];
        	var theadHtml ="";
        	var tbodyHtml = [];
        	if(columns.length>0){
        		var thHtml = [];
        		$.each(columns,function(index,data){
        			if(typeof data!="string"){
        				var normalT = [];
        				$.each(data,function(index,d){
        					var r = '<tr><th class="n_key">'+ index + '</th><td>'+ d +'</td></tr>';
        					normalT.push(r);
        				});
        				data = '<table class="r_nordiv">' + normalT.join(" ") + '</table>';
        			}
        			var th = '<th>'+ data +'</th>';
        			thHtml.push(th);
        		});        		
        		theadHtml = '<thead><tr>' + thHtml.join(" ") + '</tr></thead>';
        	}
        	
        	$.each(row_d,function(index,data){
        		var i = data.length;
        		var tdHtml = [];
        		$.each(data.row,function(idx,d){
        			if(typeof d!="string"){
        				var normalT = [];
        				$.each(d,function(index,_d){
        					var r = '<tr><th class="n_key">'+ index + '</th><td>'+ _d +'</td></tr>';
        					normalT.push(r);
        				});
        				d = '<table class="r_nordiv">' + normalT.join(" ") + '</table>';
        			}
        			var td = '<td>'+ d +'</td>';
        			tdHtml.push(td);
        		});
        		var tbodyTr = '<tr>'+ tdHtml.join(" ") +'</tr>';
				tbodyHtml.push(tbodyTr);
        	});

        	var tableHtml = '<table class="normalTable">' + 
        			theadHtml + 
        			'<tbody>'+
        			tbodyHtml.join(" ")+
        			'</tbody>'+
        			'</table>';

    		return tableHtml;
        }
    }
    return asciiTableService;
}]);